package com.lottery.im.module.service.presenter;

import android.app.Activity;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.ShowTipObserver;
import com.lottery.im.api.result.entity.msg_bulletin.BulletionDetails;
import com.lottery.im.api.result.entity.msg_bulletin.MessageDetails;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.service.view.MessageAndBulletinDetailView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

public class MessageAndBulletinDetailPresenter extends BaseFragmentPresenter<MessageAndBulletinDetailView> {

  String token = MyApplication.getInstance().getToken();
  String userId = MyApplication.getInstance().getUserInfo().getUserId();

  public MessageAndBulletinDetailPresenter(Activity activity,
      MessageAndBulletinDetailView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  public void loadMessageDetailSuccess(long id){
    ApiClient.getApiRetrofitInstance()
        .querySystemMessageDetails(userId,token,id)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new ShowTipObserver<MessageDetails>(activity) {
          @Override public void onSuccess(MessageDetails messageDetails) {
            iView.loadMesssgeDetailSuccess(messageDetails);
          }
        });
  }

  public void loadBulletinDetailSuccess(int id){
    ApiClient.getApiRetrofitInstance()
        .queryBulletinByBulletinId(userId,token,id)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new ShowTipObserver<BulletionDetails>(activity) {
          @Override public void onSuccess(BulletionDetails bulletionDetails) {
            iView.loadBullitenDetailSuccess(bulletionDetails);
          }
        });
  }
}
